<template>
  <div class="werapp">
    <div class="login-box">
      <div class="login-img">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="" prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "../api/index";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.ruleForm).then((res) => {
            console.log(res);
            if (res.meta.status === 200) {
              localStorage.setItem("token",res.data.token);
              this.$message({
                message: res.meta.msg,
                type: "success",
              });
              this.$router.push("/");
            } else {
              this.$message.error(res.meta.msg);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.ruleForm = {};
    },
  },
};
</script>

<style lang="scss" scoped>
.werapp {
  height: 100vh;
  background: #2b4b6b;
  position: relative;
  .login-box {
    position: absolute;
    width: 450px;
    height: 300px;
    top: 50%;
    left: 50%;
    background: white;
    transform: translate(-50%, -50%);
    .login-img {
      width: 152px;
      height: 152px;
      border-radius: 50%;
      border: 1px solid #eee;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      top: -86px;
      left: 150px;
      background: white;
      img {
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .demo-ruleForm {
      margin-top: 100px;
      padding: 10px;
      margin-left: -100px;
      text-align: center;
    }
  }
}
</style>